/*
 * SuperblockInfo.h
 *
 *  Created on: Jun 14, 2013
 *      Author: martijn
 */

#ifndef SUPERBLOCKINFO_H_
#define SUPERBLOCKINFO_H_

// Our own classes
#include "Device.h"		// the "device driver"
#include "Block.h"		// the "datablocks"

// C++ hook to C functions
#include <ctime>		// for: ctime()
						// Converts a unix timestamp (seconds since 1970)
						// into human readable text e.g. "Wed Jun 30 21:49:08 1993\n".
						// The total length will always be 25+1 characters.
#include <cstddef>		// for: offsetof(datatype,attribute)
						// With 'offsetof' you can get the relative position
						// of an attribute of a class.
						// Handy when converting raw diskdata into whatever that
						// data represents.
#include <cstdio>	    // for: printf()
#include <cstdlib>		// for: EXIT_SUCCESS, EXIT_FAILURE

// C++/STL specific
#include <iostream>	    // for: std::cout, std::endl

// The include files for the unix 7-th Edition Filesystem
#include "e7filsys.h"	// the description of the information in the superblock
#include "e7fblk.h"		// a "free-block" description (not used here)
#include "e7ino.h"	    // the description of the on-disk version of an inode
#include "e7dir.h"		// the description of a directory entry (not used here)
#include "e7types.h"

#include "asserts.h"	// for: notreached()

namespace std {

class FileSystem {
private:
	filsys	fs;
	Device  device;
	Block  *sp;
public:
	enum BlkUse
	{
		B_UNKNOWN,								// default status
		B_BOOT, B_SUPER, B_INODE,				// covers the first part of the medium
		B_DATA, B_INDIR1, B_INDIR2, B_INDIR3,	// known used blocks in the data area
		B_FREE									// known unused blocks in the data area
	};

	map<daddr_x,BlkUse>  usage;
	map<string,ino_x> mapStructure; //map wich contains the structure of dir/file names

	FileSystem(const char *floppie);
	virtual ~FileSystem();
	string getFileSystemName();
	string getFileSystemPack();

	void useBlock(daddr_x bno, BlkUse use);
	void reportUsage();

	void readFreeList(Device& device, filsys& fs);

	void readInode(ino_x inum);

	void printSuperBlockInfo();
	void printDumpOfFreelist();
	void printDumpOfInodesAndDirs();

	void registerBlocks(daddr_x da[], off_x size, bool isDir);
	void registerIndir(daddr_x addr, int level, off_x& size, bool isDir);
	void readDirectory( daddr_x addr);
	void printDirectory(ino_x);
	void charter(const char *floppie);
};

} /* namespace std */
#endif /* SUPERBLOCKINFO_H_ */
